package learn.stack;

/**
 * @author baoshi
 */
public class Calculate {
    public static void main(String[] args) throws Exception {
        String expression = "30+2*6-3";
        ArrayStack2 numberStack = new ArrayStack2(4);
        ArrayStack2 operStack = new ArrayStack2(3);
        int index = 0;//扫描索引
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;//临时计算结果
        char ch = ' ';//每次循环得到的字符
        String keepNum = "";
        do {
            ch = getCh(expression, index, index + 1);
            if (ArrayStack2.isOper(ch)) {//如果是运算符
                if (operStack.isEmpty()) {//如果符号栈为空，直接入栈
                    operStack.push(ch);
                } else {//如果符号栈不为空，比较当前ch与栈顶ch的优先级
                    if (ArrayStack2.priority(ch) > ArrayStack2.priority(operStack.lookTop())) {//当前符号优先级大于栈顶的优先级，直接入符号栈
                        operStack.push(ch);
                    } else {//当前符号优先级小于或等于栈顶的优先级，pop两次，拿栈顶的符号计算
                        num1 = numberStack.pop();
                        num2 = numberStack.pop();
                        oper = operStack.pop();
                        res = ArrayStack2.cal(num1, num2, oper);
                        numberStack.push(res);//计算结果入数字栈
                        operStack.push(ch);//当前符号入符号栈
                    }
                }
            } else {
//                numberStack.push(ch - 48);//ASCII 字符集转换为数组-48
                keepNum+=ch;
                if (isEnd(expression, index)) {// 取到最后一位，直接入栈
                    numberStack.push(Integer.parseInt(keepNum));
                } else {//取到最后一位，直接入栈
                    char nextCh = getCh(expression, index + 1, index + 2);//获取当前位数的下一位
                    if (ArrayStack2.isOper(nextCh)) {//如果下一个字符是运算符则入栈
                        numberStack.push(Integer.parseInt(keepNum));
                        keepNum = ""; //这个数入栈后必须置为“”
                    }
                }
            }
            index++;
        } while (index < expression.length());

        while (!operStack.isEmpty()) {   //符号栈为空，则计算结束
            num1 = numberStack.pop();
            num2 = numberStack.pop();
            oper = operStack.pop();
            res = ArrayStack2.cal(num1, num2, oper);
            numberStack.push(res);
        }
        System.out.printf("计算结果为%d: ", numberStack.pop());
    }

    /**
     * 按照指针从字符串中取出一个字符
     * @param str 待计算的字符串
     * @param index 字符串指针
     * @param i 取的位数=1
     * @return ch 取到的字符
     */
    private static char getCh(String str, int index, int i) {
        return str.substring(index, i).charAt(0);
    }

    /**
     * 字符串是否遍历到了最后
     * @param str 待计算的字符串
     * @param index 字符串指针
     * @return  boolean
     */
    private static boolean isEnd(String str, int index) {
        return index == str.length() - 1;
    }
}

/**
 * 表示栈结构的类
 */
class ArrayStack2 {
    /**
     * 栈的大小
     */
    private int maxSize;
    /**
     * 数组模拟栈，数据保存在数组中
     */
    private int[] stack;

    /**
     * 栈顶
     */
    private int top = -1;

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int[] getStack() {
        return stack;
    }

    public void setStack(int[] stack) {
        this.stack = stack;
    }

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    /**
     * 判断栈是否已满
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 判断栈空
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 入栈
     *
     * @param num 入栈的参数
     * @throws Exception 栈满
     */
    public void push(int num) throws Exception {
        if (isFull()) {
            throw new RuntimeException("栈满");
        }
        top++;
        stack[top] = num;
    }

    /**
     * @return 出栈
     * @throws Exception 栈空
     */
    public int pop() throws Exception {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int num = stack[top];
        top--;
        return num;
    }

    /**
     * 遍历栈
     */
    public void list() throws RuntimeException {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }

        for (int i = top; i > -1; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }

    /**
     * 返回运算符的优先级
     * 使用数字表示，数字越大，优先级越高
     */
    public static int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;//表达式只有+-*/
        }
    }

    /**
     * 判断是否为运算符
     */
    public static boolean isOper(char value) {
        return value == '+' || value == '-' || value == '*' || value == '/';
    }

    public static int cal(int num1, int num2, int oper) {
        int result = 0; //计算的结果
        switch (oper) {
            //注意计算顺序 num2 num1
            case '+':
                result = num2 + num1;
                break;
            case '-':
                result = num2 - num1;
                break;
            case '*':
                result = num2 * num1;
                break;
            case '/':
                result = num2 / num1;
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * 看一眼栈顶的数据
     *
     * @return int num
     */
    public int lookTop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        return stack[top];
    }
}